package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCommonObjectProperty;
import com.ruoyi.system.service.ISysCommonObjectPropertyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公共关联字段Controller
 * 
 * @author ruoyi
 * @date 2022-08-09
 */
@RestController
@RequestMapping("/system/commonObjectProperty")
public class SysCommonObjectPropertyController extends BaseController
{
    @Autowired
    private ISysCommonObjectPropertyService sysCommonObjectPropertyService;

    /**
     * 查询公共关联字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCommonObjectProperty sysCommonObjectProperty)
    {
        startPage();
        List<SysCommonObjectProperty> list = sysCommonObjectPropertyService.selectSysCommonObjectPropertyList(sysCommonObjectProperty);
        return getDataTable(list);
    }

    /**
     * 导出公共关联字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:export')")
    @Log(title = "公共关联字段", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysCommonObjectProperty sysCommonObjectProperty)
    {
        List<SysCommonObjectProperty> list = sysCommonObjectPropertyService.selectSysCommonObjectPropertyList(sysCommonObjectProperty);
        ExcelUtil<SysCommonObjectProperty> util = new ExcelUtil<SysCommonObjectProperty>(SysCommonObjectProperty.class);
        return util.exportExcel(list, "公共关联字段数据");
    }

    /**
     * 获取公共关联字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysCommonObjectPropertyService.selectSysCommonObjectPropertyById(id));
    }

    /**
     * 新增公共关联字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:add')")
    @Log(title = "公共关联字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCommonObjectProperty sysCommonObjectProperty)
    {
        return toAjax(sysCommonObjectPropertyService.insertSysCommonObjectProperty(sysCommonObjectProperty));
    }

    /**
     * 修改公共关联字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:edit')")
    @Log(title = "公共关联字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCommonObjectProperty sysCommonObjectProperty)
    {
        return toAjax(sysCommonObjectPropertyService.updateSysCommonObjectProperty(sysCommonObjectProperty));
    }

    /**
     * 删除公共关联字段
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:remove')")
    @Log(title = "公共关联字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysCommonObjectPropertyService.deleteSysCommonObjectPropertyByIds(ids));
    }

    /**
     * 新增字段判重
     */
    @PreAuthorize("@ss.hasPermi('system:commonObjectProperty:add')")
    @GetMapping("commonAddRemovingRepeat/{nameEn}")
    public AjaxResult commonAddRemovingRepeat(@PathVariable("nameEn") String nameEn){
        List<SysCommonObjectProperty> commonDataPropertyList = sysCommonObjectPropertyService.selectSysCommonObjectPropertyList(null);
        SysCommonObjectProperty sysCommonDataProperty = commonDataPropertyList.stream().filter(x -> nameEn.equals(x.getNameEn())).findFirst().orElse(null);
        if(sysCommonDataProperty != null){
            return success().put("msg", "字段名不可用，已存在该公共字段");
        }
        return success().put("msg", "success");
    }
}
